package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderListService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.internal.ir.IfNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author 2小组冯小丹
 */
@RestController
@RequestMapping(path = "/order")
@CrossOrigin
@Slf4j
public class OrderListController {

    @Reference
    private OrderListService orderListService;


    private static final ObjectMapper MAPPER = new ObjectMapper();


    /**
     * 查询预约信息
     *
     * @param currentPage
     * @param pageSize
     * @param total
     * @param queryString
     * @param minTime
     * @param maxTime
     * @param orderType
     * @param orderStatus
     * @param payStatus
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result queryOrderList(Integer currentPage,//当前页码
                                 Integer pageSize,//每页数量
                                 Integer total,//总记录数
                                 String queryString,//查询条件
                                 @DateTimeFormat(pattern = "yyyy-MM-dd")
                                         Date minTime,//时间左
                                 @DateTimeFormat(pattern = "yyyy-MM-dd")
                                         Date maxTime,//时间右
                                 String orderType,//预约类型--微信预约
                                 String orderStatus,//预约状态
                                 Integer payStatus//支付状态
    ) {

        PageResult pageResult = null;
        try {
            pageResult = orderListService.queryOrderList(currentPage,//当前页码
                    pageSize,//每页数量
                    total,//总记录数
                    queryString,//查询条件
                    minTime,//时间左
                    maxTime,//时间右
                    orderType,//预约类型--微信预约
                    orderStatus,//预约状态
                    payStatus//支付状态
            );
//            System.out.println("支付状态================="+payStatus);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, pageResult);
        } catch (Exception e) {
            log.error("出错");
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL, pageResult);
        }
    }

    /**
     * 保存预约信息
     *
     * @param fromData
     * @return
     */
    @PostMapping("/submit")
    public Result saveOrder(@RequestBody String fromData) {

        try {
            Order order = MAPPER.readValue(fromData, Order.class);

            boolean flag = orderListService.saveOrder(order);
            if (flag) {
                return new Result(true, MessageConstant.ORDER_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ORDER_FAIL);
            }
        } catch (JsonProcessingException e) {
            log.error("json转换出错");
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    @GetMapping("/{id}")
    public Result queryOrderList(@PathVariable("id") String id) {
        Order order = null;
        try {
            if (id ==null){
                return new Result(false, MessageConstant.QUERY_ORDER_FAIL, order);
            }
            order= orderListService.queryOrderById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            log.error("出错");
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL, order);
        }
    }


}